﻿using AbpApplication.InPut;
using AbpToDomain.View;
using CommunityToolkit.Mvvm.Input;
using HandyControl.Controls;
using NewWms.Apiservice;
using NewWms.Apiservice.Dto;
using NewWms.Apiservice.InPut;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using MessageBox = HandyControl.Controls.MessageBox;

namespace NewWms.ViewModels
{
    public partial class BoxConfigViewModel : BindableBase, INavigationAware
    {

        private Prism.Services.Dialogs.IDialogService Dialog;
        public DelegateCommand<string> ShowDialogCommand { get; set; }
        public DelegateCommand<string> Command {  get; set; }
        public DelegateCommand<string> DeleteCommand { get; set; }
        public DelegateCommand<string> SelectionChangedCommand { get; set; }
        private readonly IViewService viewService;
        private readonly IBaseService baseService;
        private InPut_ViewService inPut_ViewService;
        public InPut_ViewService InPut_ViewService
        {
            get { return inPut_ViewService; }
            set { inPut_ViewService = value; RaisePropertyChanged(); }
        }
        private InPut_ViewService2 inPut_ViewService2;
        public InPut_ViewService2 InPut_ViewService2
        {
            get { return inPut_ViewService2; }
            set { inPut_ViewService2 = value; RaisePropertyChanged(); }
        }

        private InPut_BaseService8 inPut_BaseService8;

        public InPut_BaseService8 InPut_BaseService8
        {
            get { return inPut_BaseService8; }
            set { inPut_BaseService8 = value;RaisePropertyChanged();}
        }

        private long count;
        public long Count
        {
            get { return count; }
            set { count = value; RaisePropertyChanged(); }
        }
        private long pagecount;
        public long PageCount
        {
            get { return pagecount; }
            set { pagecount = value; RaisePropertyChanged(); }
        }
        private long count2;
        public long Count2
        {
            get { return count; }
            set { count = value; RaisePropertyChanged(); }
        }
        private long pagecount2;
        public long PageCount2
        {
            get { return pagecount2; }
            set { pagecount2 = value; RaisePropertyChanged(); }
        }
        private List<View_BoxDto> view_BoxDtos;
        public List<View_BoxDto> View_BoxDtos
        {
            get { return view_BoxDtos; }
            set { view_BoxDtos = value;RaisePropertyChanged(); }
        }
        private List<View_BoxGrid_MaterialDto> view_BoxGrid_MaterialDtos;
        public List<View_BoxGrid_MaterialDto> View_BoxGrid_MaterialDtos
        {
            get { return view_BoxGrid_MaterialDtos; }
            set { view_BoxGrid_MaterialDtos = value; RaisePropertyChanged(); }
        }

        public BoxConfigViewModel(Prism.Services.Dialogs.IDialogService dialog)
        {  
            viewService = new ViewService();
            baseService = new BaseService();
            ShowDialogCommand = new DelegateCommand<string>(ShowDialog);
            Command = new DelegateCommand<string>(Method);
            SelectionChangedCommand = new DelegateCommand<string>(SelectionChanged);
            DeleteCommand = new DelegateCommand<string>(DeleteBox);
            InPut_ViewService= new InPut_ViewService();
            InPut_ViewService2=new InPut_ViewService2();
            InPut_BaseService8 = new InPut_BaseService8();
            View_BoxDtos = new List<View_BoxDto>();
            View_BoxGrid_MaterialDtos = new List<View_BoxGrid_MaterialDto>();
            Dialog = dialog;
            InPut_ViewService.PageIndex = 1;
            InPut_ViewService.PageSize = 50;
            InPut_ViewService2.PageIndex = 1;
            InPut_ViewService2.PageSize = 50;
            ViewBox();
            View_BoxGrid_Material(null);
        }

        public void Method(string obj)
        {
            switch (obj)
            {
                case "ViewBox":ViewBox();break;
                case "View_BoxGrid_Material": View_BoxGrid_Material(null);break;
                case "DeleteBoxs": DeleteBoxs();break;




            }
        }
        private  async void ViewBox()
        {
           
            Tuple<List<View_BoxDto>, long?, long?> tuple = await viewService.View_Box(InPut_ViewService);
            if (tuple == null)
            {
                return;
            }
            View_BoxDtos = tuple.Item1;
            Count = (long)tuple.Item2;
            PageCount = (long)tuple.Item3;
        }
        private async void SelectionChanged(string?BoxCode)
        {
            View_BoxGrid_Material(BoxCode);
        }
        private async void View_BoxGrid_Material(string? BoxCode)
        {
            InPut_ViewService2.BoxCode = BoxCode;
            Tuple<List<View_BoxGrid_MaterialDto>, long?, long?> tuple = await viewService.View_BoxGrid_Material(InPut_ViewService2);
            if (tuple == null)
            {
                return;
            }
            View_BoxGrid_MaterialDtos = tuple.Item1;
            Count2 = (long)tuple.Item2;
            PageCount2 = (long)tuple.Item3;
        }
         
        private async void DeleteBox(string obj)
        {
            InPut_BaseService8.BoxCode= obj;
            bool res=  await baseService.DeleteBox(InPut_BaseService8);
            if (res) { ViewBox(); }
        }
        private async void DeleteBoxs()
        {
            
            if(string.IsNullOrEmpty( InPut_BaseService8.BoxCode)&& string.IsNullOrEmpty(InPut_BaseService8.EndBoxCode))
            {
                MessageBox.Info("请填写完整");
                return;
            }
            bool res = await baseService.DeleteBox(InPut_BaseService8);
            if (res) { ViewBox(); }
        }

        [RelayCommand]//更新窗口弹窗
       private void UpdateBoxDialog(string obj)
        {
            if (string.IsNullOrEmpty(obj))
            {
                MessageBox.Info("请选择料盒");
                return;
            }
           DialogParameters dialogParameters = new DialogParameters();
            dialogParameters.Add( "1",obj);
            Dialog.ShowDialog("UpdateBoxDialog", dialogParameters, (r =>
            {
                var resulit = r.Result;
            }));
        }

       private void ShowDialog(string obj)
        {
            Dialog.ShowDialog(obj);

        }
        private string title = "箱号管理";
        public string Title
        {
            get { return title; }
            set { title = value;RaisePropertyChanged(); }
        }
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            
        }
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            ViewBox();
            View_BoxGrid_Material(null);
        }
    }
}
